//
//  JZHCEView.m
//  极致荟
//
//  Created by hetao on 14-5-8.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHCEView.h"
#import "JZHCETableViewCell.h"

#import "JZHAppDelegate.h"

@implementation JZHCEView
CGFloat bigenX;//手势开始坐标
UIPanGestureRecognizer *swi;
UITapGestureRecognizer *tap;
int sty;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



- (JZHCEView *)initarr:(NSArray *)arr viewConetroller:(UIViewController *)controller TopviewStype:(int)stype
{
    JZHCEView *ce=[[NSBundle mainBundle]loadNibNamed:@"JZHCEView" owner:self options:nil][0];
    ce.controller=controller;
    ce.arr=arr;
    [ce setFrame:CGRectMake(0, 0, ce.frame.size.width, ce.frame.size.height)];
    JZHAppDelegate *app=(JZHAppDelegate *)[[UIApplication sharedApplication] delegate];
    [app.view addSubview:ce];
    [ce addGesture];
    ce.agtime=0.3;
    [ce.sotext setReturnKeyType:UIReturnKeySearch];
    [ce.sotext setDelegate:ce];
    sty=stype;
   
    ce.topview.hidden=NO;
   
    return ce;
    
}


- (IBAction)alldata:(id)sender
{
     [self right];
    self.rection(0);
    self.alldata();
}

///左滑动
- (void)left
{
      view.hidden=NO;
    [UIView animateWithDuration:self.agtime animations:^{
         [self.controller.view setFrame:CGRectMake(self.frame.size.width, 0, self.controller.view.frame.size.width, self.controller.view.frame.size.height)];
       
    }];
   
}

///右滑动
- (void)right
{
      view.hidden=YES;
    [UIView animateWithDuration:self.agtime animations:^{
          [self.controller.view setFrame:CGRectMake(0, 0, self.controller.view.frame.size.width, self.controller.view.frame.size.height)];
        
    }];
  
}

///添加滑动手势
- (void)addGesture
{
   swi=[[ UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    swi.delegate=self;
    tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    
    ///给所控制的界面添加一层透明的VIew 防止不当操作
    view=[[UIView alloc]initWithFrame:CGRectMake(0, 60, 320,HEIGHT )];
    [view addGestureRecognizer:tap];
    [self.controller.view addGestureRecognizer:swi];
    [self.controller.view addSubview:view];
    [view setHidden:YES];

    
}

- (void)remove
{
    [self.controller.view removeGestureRecognizer:swi];
    [view removeGestureRecognizer:tap];
    [view removeFromSuperview];
    [self removeFromSuperview];
}
- (void)tap:(UITapGestureRecognizer *)tap
{
    [self right];
    self.rection(0);
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
///手势代理
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    bigenX=[gestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow].x;
    return YES;
}


- (void)swipe:( UIPanGestureRecognizer *)siw
{
    [self gesturechu:bigenX end:[siw locationInView:[UIApplication sharedApplication].keyWindow].x];
    bigenX=[siw locationInView:[UIApplication sharedApplication].keyWindow].x ;
    
    if([siw numberOfTouches]==0)
    {
        [self genend];
    }
    
}

///当手指离开屏幕时
- (void)genend
{
    self.agtime=0.2;
    if(self.controller.view.frame.origin.x<=self.frame.size.width/2)
    {
        [self right];
      
        self.rection(0);
    }else
    {
        [self left];
        self.rection(1);
        
    }
    self.agtime=0.3;
}
///手势滑动时界面操作
- (void)gesturechu:(CGFloat)bigen end:(CGFloat)end
{

   
        ///由于下次改变View坐标会超出规定范围，这里就多加了几判断条件，暂时没有想到更好的方法，人笨了
               [UIView animateWithDuration:0 animations:^{
                   if(self.controller.view.frame.origin.x-(bigenX-end)<self.frame.size.width && self.controller.view.frame.origin.x-(bigenX-end)>0)
                   {
                   [self.controller.view setFrame:CGRectMake(self.controller.view.frame.origin.x-(bigenX-end), 0, self.controller.view.frame.size.width, self.controller.view.frame.size.height)];
                   }else if(self.controller.view.frame.origin.x-(bigenX-end)>self.frame.size.width)
                   {
                        [self.controller.view setFrame:CGRectMake(self.frame.size.width, 0, self.controller.view.frame.size.width, self.controller.view.frame.size.height)];
                       self.rection(1);
                   }else if(self.controller.view.frame.origin.x-(bigenX-end)<0)
                   {
                        [self.controller.view setFrame:CGRectMake(0, 0, self.controller.view.frame.size.width, self.controller.view.frame.size.height)];
                        self.rection(0);
                   }
                   
                   
               }];
            
    
}


///搜索框搜索代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [self right];
    self.rection(0);
    if(self.ceDelete)
    {
        [self.ceDelete CESo:textField.text];
    }
    
    return YES;
}
- (void)panGesturUserEnable:(BOOL)end
{
    [swi setEnabled:end];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return _arr.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str=@"adf";
    JZHCETableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if(!cell)
    {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"JZHCETableViewCell" owner:self options:nil] objectAtIndex:0];
    }

    cell.lable.text=[_arr objectAtIndex:indexPath.row];
          [cell.lable setTextColor:[UIColor whiteColor]];
         [cell.xian setBackgroundColor:[UIColor colorWithRed:40/255.0 green:40/255.0 blue:40/255.0 alpha:1]];
        [cell.contentView setBackgroundColor:[UIColor colorWithRed:51/255.0 green:51/255.0  blue:51/255.0  alpha:1]];
    

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 41;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self rection];
     self.rection(0);
    self.pusblock(indexPath.row);
    
}
@end
